﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Diagnostics;
using System.Windows.Media.Effects;
using Workflow.Silverlight.Controls;
using System.Windows.Threading;

namespace WorkflowDesigner.Workflow.Item
{
    public partial class FlowEnd : WorkflowBase
    {
        // Tooltip
        protected string m_strComment = null;

        // 默认的填充颜色
        protected Color m_clrText = Colors.Black;
        protected Color m_clrFocus = Colors.Yellow;
        protected Color m_clrMouseOver = Color.FromArgb(255, 255, 0, 0);
        protected Color m_clrNormal = Color.FromArgb(255, 200, 0, 0);
        protected Color m_clrShardow = Color.FromArgb(255, 100, 100, 100);
        // 默认的边框颜色
        protected Color m_clrBorder = Colors.Black;

        public FlowEnd()
        {
            InitializeComponent();
            OnInit();
        }

        protected override void OnInit()
        {
            base.OnInit();

            // 内圈 
            sNodeInner.Width = this.Width - 6.0;
            sNodeInner.Height = this.Height - 6.0;
            sNodeInner.Fill = new SolidColorBrush(m_clrNormal);

            // 外圈
            sNodeOutter.Stroke = new SolidColorBrush(m_clrBorder);
            sNodeOutter.Width = this.Width;
            sNodeOutter.Height = this.Height;

            // 阴影效果
            sNodeShadow.Width = this.Width;
            sNodeShadow.Height = this.Height;
            BlurEffect blur = new BlurEffect();
            blur.Radius = Utility.dBlurEffectRadius;
            sNodeShadow.Effect = blur;
            sNodeShadow.Fill = new SolidColorBrush(m_clrShardow);

            // 文本区域
            txtLable.MaxWidth = this.Width;
            txtLable.Foreground = new SolidColorBrush(m_clrText);
            txtLable.SetValue(Canvas.TopProperty, (this.Height - txtLable.ActualHeight) / 2.0);
            txtLable.SetValue(Canvas.LeftProperty, (this.Width - txtLable.ActualWidth) / 2.0);

            this.SizeChanged += new SizeChangedEventHandler(Node_SizeChanged);

            sNodeShadow.SetValue(Canvas.ZIndexProperty, 1);
            sNodeOutter.SetValue(Canvas.ZIndexProperty, 2);
            sNodeInner.SetValue(Canvas.ZIndexProperty, 3);
            txtLable.SetValue(Canvas.ZIndexProperty, 4);
        }

        private void Node_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            if (this.Width != this.Height)
            {
                double dMax = Math.Max(this.Height, this.Width);
                this.Height = this.Width = dMax;
            }
            else
            {
                sNodeOutter.Width = this.Width;
                sNodeOutter.Height = this.Height;
                sNodeInner.Width = this.Width - 6.0;
                sNodeInner.Height = this.Height - 6.0;
                sNodeInner.SetValue(Canvas.TopProperty, 3.0);
                sNodeInner.SetValue(Canvas.LeftProperty, 3.0);
                sNodeShadow.Width = this.Width;
                sNodeShadow.Height = this.Height;
                txtLable.SetValue(Canvas.TopProperty, (this.Height - txtLable.ActualHeight) / 2.0);
                txtLable.SetValue(Canvas.LeftProperty, (this.Width - txtLable.ActualWidth) / 2.0);
                App.theMainView.TryResize();
                if (this.OnControlMove != null)
                {
                    this.OnControlMove(this);
                }
            }
        }

        public override bool IsFocus
        {
            get
            {
                return m_isFocus;
            }
            set
            {
                m_isFocus = value;
                if (m_isFocus)
                {
                    // 焦点状态
                    sNodeShadow.Fill = new SolidColorBrush(m_clrFocus);
                    sNodeInner.Fill = new SolidColorBrush(m_clrFocus);
                    App.theMainView.SetFlowItemFocus(this);
                    this.SetValue(Canvas.ZIndexProperty, App.NextMaxIndex);
                }
                else
                {
                    sNodeInner.Fill = new SolidColorBrush(m_clrNormal);
                    sNodeShadow.Fill = new SolidColorBrush(m_clrShardow);
                }
            }
        }

        protected override void OnMouseEnter(MouseEventArgs e)
        {
            base.OnMouseEnter(e);
            this.IsMouseOver = true;
            this.Cursor = Cursors.Hand;
            if (!IsUseForToolBox && !IsFocus)
            {
                sNodeShadow.Fill = new SolidColorBrush(m_clrMouseOver);
                sNodeInner.Fill = new SolidColorBrush(m_clrMouseOver);
            }
        }

        protected override void OnMouseLeave(MouseEventArgs e)
        {
            base.OnMouseLeave(e);
            this.IsMouseOver = false;
            this.Cursor = Cursors.Arrow;
            if (!IsUseForToolBox && !IsFocus)
            {
                sNodeShadow.Fill = new SolidColorBrush(m_clrShardow);
                sNodeInner.Fill = new SolidColorBrush(m_clrNormal);
            }
        }

        public override Point GetCrossPoint(Point ptOuterPoint)
        {
            if (this.Width != this.Height)
            {
                double dMax = Math.Max(this.Height, this.Width);
                this.Height = this.Width = dMax;
            }
            Point ptCenter = new Point(this.Width / 2, this.Height / 2);
            Point ptCross = Utility.GetRoundCrossPoint(ptCenter, this.Width / 2, ptOuterPoint);
            return ptCross;
        }

        public override FlowItemType ItemType
        {
            get
            {
                return FlowItemType.End;
            }
        }
    }
}
